<?php
/**
 * 积分余额管理
 * @author Administrator
 *
 */
class ownerIntegral extends CControllerMerchantBase{
	
	public function show()
	{
		$this->_title="积分余额管理";
		
		$icModel = new integralChangeModel();
		$where = array();
		
		$s_type=$this->getRequestData("type","get");
		$s_v=$this->getRequestData("sv","get");
		
		$where = "shop_id=".$this->_shopID;

		if($s_type && $s_v)
		{
			$this->setViewParam("type", $s_type);
			$this->setViewParam("s_v", $s_v);
			if($s_type=="card")
			{
				$where .= " and owner_card='".$s_v."'";
			}
			else{
				$where .= " and owner_name='".$s_v."'";
			}
		}
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;

		$list = $icModel->selectPage($pageIndex,$pageSize,
			"owner_name,owner_card,cost_money_t,discount_num,c_discount_type,c_discount,owner_id",$where,"","",$c,"V_OWNER_INTEGRAL");
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c);

		$this->render("owner_integral/list",array("url"=>"/merchant/ownerIntegral/show", "list"=>$list));
	}
	
	public function detail()
	{
		$this->_title = "积分余额明细";
		
		$s_type=$this->getRequestData("type","get");
		$s_v=$this->getRequestData("sv","get");
		
		$ownerID=$this->getRequestData("oid","get");
		
		if(!CCommon::checkNum($ownerID))
		{
			$this->sysMsg("ID无效",1);
		}

		$icModel = new integralChangeModel();
		
		if($s_type && $s_v)
		{
			$this->setViewParam("type", $s_type);
			$this->setViewParam("s_v", $s_v);
			if($s_type=="card")
			{
				$whereOwner["supplementary_card"] = $s_v;
			}
			else{
				$whereOwner["owner_name"]=$s_v;
			}
			
			$ownerInfo = $icModel->selectByWhere("t_owner","owner_id",$whereOwner, "card_status=1 and del_flag=0");
			
			if($ownerInfo)
			{
				$ownerID = $ownerInfo["owner_id"];
			}
			else{
				$this->sysMsg("业主不存在",1);
			}
		}
	
		$ownerInfo = $icModel->selectByWhere(
			"t_owner o,t_community c","o.*,c.community_name,c.province,c.city,c.area",array("owner_id"=>$ownerID), 
			"o.card_status=1 and o.community_id=c.community_id and o.del_flag=0");
		
		$where["owner_id"] = $ownerID;
		$where["shop_id"] = $this->_shopID;
		
		$ownerShopRe = $icModel->selectByWhere(
			"t_owner_shop",
			"os_id,owner_id,shop_id,balance,owner_card_status,c_discount_type,c_discount",
			$where,"del_flag=0");
			
		$ownerTj=$icModel->selectByWhere("V_OWNER_INTEGRAL",
			"cost_money_t,discount_num",$where);
			
		if($ownerShopRe)
		{
			$hasRef = 1;
			if($ownerShopRe["c_discount_type"]==1)
			{
				$this->setViewParam("discount", "送积分");
			}
			else{
				$this->setViewParam("discount",$ownerShopRe["c_discount"]."折");
			}
			$this->setViewParam("ownerShopRe", $ownerShopRe);
		}
		else{
			$hasRef = 0;
			$this->setViewParam("discount", "未关联");
		}
		
		$this->setViewParam("hasRef", $hasRef);
		$this->setViewParam("ownerInfo", $ownerInfo);
		$this->setViewParam("ownerShopRe",$ownerShopRe);
		$this->setViewParam("ownerTj",$ownerTj);
		$this->setViewParam("ownerShopRe",$ownerShopRe);
		
		$pageIndex = $this->getRequestData("page","GET",1);
		$pageSize = 10;
		$c = 0;
		$list = $icModel->selectPage($pageIndex,$pageSize,
			"c_id,pay_id,cost_money,cost_type,cost_time,owner_card,cost_shop_before_money,cost_shop_after_money,cost_money_t,cancel_pay_id",
		"shop_id=".$this->_shopID." and owner_id=".$ownerID,"c_id desc","",$c);
		$pageStr = CPage::showPage($pageIndex,$pageSize,$c,"?oid=".$ownerID);
		
		$this->setViewParam("integralDetail",$list);
		$this->setViewParam("pageStr",$pageStr);
		
		$this->setViewParam("showBackBtn",1);
		
		$this->render("owner_integral/detail",array("url"=>"/merchant/ownerIntegral/detail?oid=".$ownerID));
	}
}